#!/usr/bin/env python3

import random

#   1. 随机1-100之间的数，由用户输入猜测，用户输入的数大则提示大，小则提示小
def randomNum():
    randomNum = random.randint(1, 101)
    round = 0
    while round < 5:
        num = int(input("请选择一个数字:"))
        if num < 1 or num > 101:
            print('您输入的数字超出了范围！')
        elif num < randomNum:
            print('您输入的数字小了。')
        elif num > randomNum:
            print('您输入的数字大了。')
        elif  num == randomNum:
            print('恭喜你，回答正确！')
        round += 1
        print('您的机会还有', (5 - round), '次')
        while round == 5:
            print('很遗憾，你的机会用完了，请重头再来！')
            break

#   2. 求1000以内的完数,完数的定义:所有因子之和等于该本身
def isPrefectNum(num):
    """
    判断输入数是否为完数
    """
    s = 0

    for i in range(1, num // 2 + 1):
        if num % i == 0:
            s += i

    return True if s == num else False

def prefectNumTest():
    """
    求1000以内的完数
    """
    for n in range(2, 1000):
        if isPrefectNum(n):
            print("%d 是完数" % (n))

#   3. 求1000以内的水仙花数(每一位的立方之和等于该数本身)
def isNarcissusNum(num):
    """
    判断输入数是否为水仙花数
    """
    n = num
    s = 0

    while n > 0:
        s += (n % 10) ** 3
        n //= 10

    return s == num

    #  if (num % 10) ** 3 + (num // 10 %10) ** 3 + (num // 100 % 100) ** 3 == num:
        #  return True
    #  return False

def narcissusNumTest():
    """
    求1000以内的水仙花数
    """
    for n in range(100, 1000):
        if isNarcissusNum(n):
            print("%d 是水仙花数" % (n))

#   4. 判断一个数是否为回文数(11 12321 1234321)
#  def isPalindromicNum(num):
    #  """
    #  判断一个数是否为回文数(11 12321 1234321)
    #  """

    #  n = num
    #  s = 0

    #  while n > 0:
        #  s = s * 10 + n % 10
        #  n //= 10

    #  return s == num

    #  #  使用字符串实现
    #  #  s = str(num)
    #  #  return int(s[::-1]) == num

#  def palindromicNumTest():
    #  num = int(input("请输入: "))

    #  if isPalindromicNum(num):
        #  print("%d 是回文数!" % (num))
    #  else:
        #  print("%d 不是回文数!" % (num))

def palindrome():
    num = str(input('请输入一个数：'))

    if num[::-1] == num:
        print('True')
    else:
        print ('False')


#  5. 打印矩形
def rect(w, h):
    print("w = %d, h = %d" % (w, h))
    for r in range(h):
        for c in range(w):
            print(" *", end='')
        print()

    print("=======================")

    for r in range(h):
        print(" *" * w)

    print("=======================")
    for i in range(w * h):
        c = i % w
        print(" *", end='')
        if c == w - 1:
            print()

#   6. 打印左上三角形(输入边长打印直角三角形用一个循环)

def triangle1():
    length = int(input('请输入边长: '))
    for i in range(length, 0, -1):
        print(' *' * i, ' ' * (length - i))
#   打印左下三角形(输入边长打印直角三角形用一个循环)

def triangle2():
    length = int(input('请输入边长: '))
    for i in range(length, 0, -1):
        print(' *' * (length - i + 1), ' ' * i)

#打印右上三角形(输入边长打印直角三角形用一个循环)

def triangle3():
    length = int(input('请输入边长: '))
    for i in range(length, 0, -1):
        print('  ' * (length - i), ' *' * i)

#打印右下三角形(输入边长打印直角三角形用一个循环)

def triangle4():
    length = int(input('请输入边长: '))
    for i in range(length, 0, -1):
        print('  ' * i, ' *' * (length - i + 1))

#   七. 4 5 6 7能组合多少个不能被4整除的4位数(位数不能相同,扩展考虑一个循环实现)
def fourBit():
    c = 0
    #  for i in range(4, 8):
        #  for j in range(4, 8):
            #  for k in range(4, 8):
                #  for l in range(4, 8):
                    #  if i == j or i == k or i == l           \
                            #  or j == k or j == l or k == l:
                        #  continue
                    #  n = i * 1000 + j * 100 + k * 10 + l
                    #  if n % 4 != 0:
                        #  print("%d " % (n), end='')
                        #  c += 1

    s = 4 + 5 + 6 + 7
    for i in range(4, 8):
        for j in range(4, 8):
            if i == j:
                continue
            for k in range(4, 8):
                if i == k or j == k:
                    continue
                l = s - i - j - k
                n = i * 1000 + j * 100 + k * 10 + l
                if n % 4 != 0:
                    print("%d " % (n), end='')
                    c += 1
    print("\nc = %d" % (c))

#控制菜单：
while True:
    print("\033[1;1H\033[2J", end='')
    print("1. 随机猜数(范围:1-100)")
    print("2. 完数")
    print("3. 水仙数")
    print("4. 回文数")
    print("5. 矩形")
    print("6. 三角形")
    print("7. 组合数")
    print("8. 退出")
    sel = int(input("请输入【1 - 8】: "))
    print("------------------------------------------------------------")
    if sel == 1:
        randomNum()
    elif sel == 2:
        prefectNumTest()
    elif sel == 3:
        narcissusNumTest()
    elif sel == 4:
        palindrome()
    elif sel == 5:
        rect(random.randint(1, 10), random.randint(1, 10))
    elif sel == 6:
        while True:
            print("1.左上直角三角形")
            print("2.左下直角三角形")
            print("3.右上直角三角形")
            print("4.右下直角三角形")
            print("5.返回上一级")
            sel = int(input("请输入:"))
            print("----------------------------------------------------")
            if sel == 1:
                triangle1()
            elif sel == 2:
                triangle2()
            elif sel == 3:
                triangle3()
            elif sel == 4:
                triangle4()
            elif sel == 5:
                break
    elif sel == 7:
        fourBit()
    elif sel == 8:
        break
    print("-----------------------------------------------------------")
    sel = input("按回车继续选择菜单, 按q退出: ")
    if sel == 'q':
        break
